import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AddRoleService} from './addRole.service';
import {BaseComponent} from '../common/base.component';
import {AddRolePage, AddRole, Authority} from './addRole';
import {NavbarService} from '../navbar/navbar.service';

declare var $: any;

@Component({
  selector: 'app-role',
  templateUrl: './addRole.component.html'
})
export class AddRoleComponent extends BaseComponent implements OnInit {
  editorTitle: String = '';
  isAddEditor: Boolean;
  curPage: Number = 1;
  addRolePage: AddRolePage;
  addRole: AddRole[];
  curaddRole: AddRole;
  authority: Authority[];
  strA: String = '';
  strB: String = '';
  strAb: String = '';
  childB = [];

  constructor(private addRoleService: AddRoleService, private navbarService: NavbarService, private router: Router) {
    super();
  }

  ngOnInit() {
    this.curaddRole = {
      name: '',
      updateTime: '',
      id: '',
      sort: '',
      remark: '',
      status: '',
      userId: '',
      createTime: ''
    };
    this.searchRole();
  }

  // 新增角色
  onAddRole() {
    this.isAddEditor = true;
    this.editorTitle = '新增角色';
    this.curaddRole = {
      name: '',
      updateTime: '',
      id: '',
      sort: '',
      remark: '',
      status: '',
      userId: '',
      createTime: ''
    };
    $('#data_editor').modal('show');
  }

// 修改
  onEditRole(value: AddRole) {
    this.isAddEditor = false;
    this.editorTitle = '修改角色';
    this.curaddRole = value ;
    $('#data_editor').modal('show'); // 显示编辑对话框

  }

// 删除
  onDeleteRole(value: AddRole) {
    this.curaddRole = value;
    $('#delete_confirm').modal('show'); // 显示编辑对话框
  }

// 新增/修改
  save() {
    if (this.isAddEditor) { // 新增数据
      this.curPage = 1;
      this.addRoleService.addURoleinfo(this.curaddRole)
        .then(result => {
          if (result) {
            this.alert.info('新增成功');
            this.queryList();
          } else {
            this.alert.error('新增失败');
          }
        });
    } else { // 修改数据
      this.addRoleService.updateRoleinfo(this.curaddRole)
        .then( result => {
          if (result) {
            this.alert.info('修改成功');
            this.queryList();
          }else {
            this.alert.error('修改失败');
            this.queryList();
          }
        });
    }

    $('#data_editor').modal('hide');
  }

  // 删除
  delete() {
    this.addRoleService.removeUserinfo(this.curaddRole)
      .then(res => {
          if (res) {
            this.alert.info('删除成功');
            this.queryList();
          } else {
            this.alert.error('删除失败');
          }
        }
      );
    $('#delete_confirm').modal('hide');
  }

  // 查询角色
  searchRole() {
    this.curPage = 1;
    this.queryList();
  }

  onPage(event) {
    this.curPage = event;
    this.queryList();
  }

  // 分配权限
  onSelPower(value: AddRole) {
    this.editorTitle = '分配权限';
    this.curaddRole = value;
    this.getMenus();
    $('#sel_power').modal('show'); // 显示分配权限对话框
  }

  private queryList() {
    this.addRoleService.getRole(this.curPage)
      .then(page => {
          this.addRolePage = page;
          this.addRole = page.addRole.datas;
        }
      );
  }
// 获取所有菜单
  getMenus() {
    this.addRoleService.getItemMenu()
      .then(res => {
        if (res[0]) {
          this.authority = res[1].userTreeTo;
          for (const index of this.authority) {
            for (const obj of index.childUserTreeTo) {
              this.childB.push(obj);
            }
          }
        }
      })
      .catch(error => this.alert.error(error));
  }

// 点击checkbox事件
  selCheckA(group) {
    group.active = !group.active;
  };

  selCheckB(router) {
    router.active = !router.active;
  };

// 点击选中的一级菜单
  findItemA() {
    let strA = '';
    for (const rgb of this.authority.filter(ele => ele.active === true)) {
      strA += rgb.id + ',';
      this.strA = strA.substr(0, strA.length - 1);
    }
  }

  // 点击选中的二级菜单
  findItemB() {
    let strB = '';
    for (const rgb of this.childB.filter(ele => ele.active === true)) {
      strB += rgb.id + ',';
      this.strB = strB.substr(0, strB.length - 1);
    }
  }

  // 保存已分配的权限
  saveAllPower() {
    this.findItemA();
    this.findItemB();
    this.strAb = this.strA + ',' + this.strB;
    this.addRoleService.allPower(this.curaddRole, this.strAb)
      .then(res => {
          if (res) {
            this.alert.info('分配角色成功');
          }
        }
      );
    $('#sel_power').modal('hide');
  }

}
